﻿using Audit.Core;
using Dapper_Core.BLL.Interface;
using Dapper_Core.Common;
using Dapper_Core.DAL.Impl;
using Dapper_Core.Model;
using Dapper_Core.Table;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;

namespace Dapper_Core.BLL.Impl
{
    public class Manufacturer_InfoBusiness : BaseRepository<Manufacturer_Info>, IManufacturer_InfoBusiness
    {
        public Manufacturer_InfoBusiness()
        {

        }

        public Manufacturer_InfoBusiness(TransactionContext transactionContext) : base(transactionContext)
        {

        }

        public bool IsExist(string manufacturerCode, string manufacturerName)
        {
            try
            {
                if (string.IsNullOrEmpty(manufacturerCode) || string.IsNullOrEmpty(manufacturerName)) return true;
                Expression<Func<Manufacturer_Info, bool>> exp = model => model.ManufacturerCode == manufacturerCode || model.ManufacturerName == manufacturerName;
                List<Manufacturer_Info> list = base.GetList(exp, false).ToList();
                if (list != null && list.Count > 0) return true;
                return false;
            }
            catch (Exception ex)
            {
                NlogUtility.WriteError(MethodBase.GetCurrentMethod().DeclaringType.FullName, "检查厂商是否存在失败", "manufacturerCode=" + manufacturerCode + "，manufacturerName=" + manufacturerName, ex, "DapperCore");
                return false;
            }
        }

        public bool Add(Manufacturer_Info manufacturer)
        {
            using (AuditScope scope = AuditScope.Create("Manufacturer_Info:Add", () => manufacturer))
            {
                try
                {
                    scope.Comment("添加单个本地签名服务器厂商");
                    scope.SetCustomField("Module", "Manufacturer_Info");
                    scope.SetCustomField("Operator", SessionUtility.GetBasicUserInfo().PinyinCode);
                    scope.SetCustomField("Operation", Operation.Insert);
                    bool success = base.Add(manufacturer);
                    if (!success)
                    {
                        scope.Discard();
                    }
                    return success;
                }
                catch (Exception ex)
                {
                    scope.Discard();
                    NlogUtility.WriteError(MethodBase.GetCurrentMethod().DeclaringType.FullName, "本地签名服务器厂商添加失败", "manufacturer=" + JsonConvert.SerializeObject(manufacturer), ex, "DapperCore");
                    return false;
                }
            }
        }

        public bool UpdateNotNull(Manufacturer_Info manufacturer)
        {
            Manufacturer_Info old = base.Get(manufacturer.Id);
            using (AuditScope scope = AuditScope.Create("Manufacturer_Info:Edit", () => old))
            {
                try
                {
                    scope.Comment("编辑单个本地签名服务器厂商项");
                    scope.SetCustomField("Module", "Manufacturer_Info");
                    scope.SetCustomField("Operator", SessionUtility.GetBasicUserInfo().PinyinCode);
                    scope.SetCustomField("Operation", Operation.Update);
                    old = manufacturer;
                    bool success = base.UpdateNotNull(manufacturer);
                    if (!success)
                    {
                        scope.Discard();
                    }
                    return success;
                }
                catch (Exception ex)
                {
                    scope.Discard();
                    NlogUtility.WriteError(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName, "本地签名服务器厂商修改失败", "manufacturer=" + JsonConvert.SerializeObject(manufacturer), ex, "DapperCore");
                    return false;
                }
            }
        }

        public (List<Manufacturer_Info> roles, int count) GetPageList(int pageIndex, int rowsPerPage, Manufacturer_Info manufacturer, string orderby)
        {
            try
            {
                if (pageIndex < 0 || rowsPerPage < 1)
                {
                    return (new List<Manufacturer_Info>(), 0);
                }
                Expression<Func<Manufacturer_Info, bool>> exp = model => model.ManufacturerCode.Contains(manufacturer.ManufacturerCode) && model.ManufacturerName.Contains(manufacturer.ManufacturerName)
                                                                         && model.CreateDate > manufacturer.CreateDateStart && model.CreateDate < (manufacturer.CreateDateEnd == null ? manufacturer.CreateDateEnd : ((DateTime)manufacturer.CreateDateEnd).AddDays(1))
                                                                         && model.Creator.Contains(manufacturer.Creator) && model.IsActive == manufacturer.IsActive;
                List<Manufacturer_Info> roles = base.GetPagelist(pageIndex + 1, rowsPerPage, exp, orderby).ToList();
                int count = base.GetListCount(exp);
                NlogUtility.WriteInfo(MethodBase.GetCurrentMethod().DeclaringType.FullName, "查询本地签名服务器厂商分页信息", "pageIndex=" + pageIndex + "，rowsPerPage=" + rowsPerPage + "，manufacturer=" + JsonConvert.SerializeObject(manufacturer) + "，orderby=" + orderby, "DapperCore");
                return (roles, count);
            }
            catch (Exception ex)
            {
                NlogUtility.WriteError(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName, "获取本地签名服务器厂商分页信息失败", "pageIndex=" + pageIndex + "，rowsPerPage=" + rowsPerPage + "，manufacturer=" + JsonConvert.SerializeObject(manufacturer), ex, "DapperCore");
                return (new List<Manufacturer_Info>(), 0);
            }
        }
    }
}
